package demo.swing.tickets;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import demo.swing.SqlQuery;
import demo.swing.passenger.EnterPassengerListener;

public class SelectTickets extends JFrame implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = -2541226449088145660L;
	private JTextArea infoPassengerFlightBalls = null;
	private JButton purshaceBisnesWithBallsBtn = null;
	private JButton purshaceBisnesWithoutBallsBtn = null;
	private JButton purshaceEconomWithBallsBtn = null;
	private JButton purshaceEconomWithoutBallsBtn = null;
	private EnterPassengerListener allInfo;
	private void makeButton(JButton btn, GridBagLayout layout, GridBagConstraints cs){
		layout.setConstraints(btn, cs);
		add(btn);
		return ;
	}
	private void updateBonus(String passengerID, Integer price){
		String query = "update passenger set bonus = bonus + 0.3 * " + price + " where id = '" + passengerID + "';";
		allInfo.getSql().executeSqlUpdate(query);
		return ;
	}
	private void clearBonus(String passengerID, Integer price){
		String query = "update passenger set bonus = ";
		if (price <= Integer.valueOf(allInfo.getBonus())){
			query += "bonus - " + price + " where id = '" + passengerID + "';";
		}else{
			query += "0 where id = '" + passengerID + "';";
		}
		allInfo.getSql().executeSqlUpdate(query);
		return;
	}
	public SelectTickets(String title, EnterPassengerListener l){
		super(title);
		allInfo = l;
		infoPassengerFlightBalls = new JTextArea(allInfo.toString());
		purshaceBisnesWithBallsBtn = new JButton("Buy a ticket in business class using bonus points.");
		purshaceBisnesWithoutBallsBtn = new JButton("Buy a ticket in business class without using bonus points.");
		purshaceEconomWithBallsBtn = new JButton("Buy a ticket in economy class without using bonus points.");
		purshaceEconomWithoutBallsBtn = new JButton("Buy a ticket in economy class using bonus points.");
		purshaceBisnesWithBallsBtn.addActionListener(this);
		purshaceBisnesWithoutBallsBtn.addActionListener(this);
		purshaceEconomWithBallsBtn.addActionListener(this);
		purshaceEconomWithoutBallsBtn.addActionListener(this);
		if (Integer.valueOf(allInfo.getBonus()) < 1){
			purshaceBisnesWithBallsBtn.setEnabled(false);
			purshaceEconomWithBallsBtn.setEnabled(false);
		};
		if (Integer.valueOf(allInfo.getFlight().getTicketsOnBisnesClass()) < 1){
			purshaceBisnesWithBallsBtn.setEnabled(false);
			purshaceBisnesWithoutBallsBtn.setEnabled(false);
		}
		if (Integer.valueOf(allInfo.getFlight().getTicketsOnEconomClass()) < 1){
			purshaceEconomWithBallsBtn.setEnabled(false);
			purshaceEconomWithoutBallsBtn.setEnabled(false);
		}
		GridBagLayout layout = new GridBagLayout();
		setLayout(layout);
		GridBagConstraints c = new GridBagConstraints();
		c.gridheight = 1;
		c.fill = GridBagConstraints.BOTH;
		c.gridwidth = GridBagConstraints.REMAINDER;
		JScrollPane scrollTextArea = new JScrollPane(infoPassengerFlightBalls);
		layout.setConstraints(scrollTextArea, c);
		this.add(scrollTextArea);
		c = new GridBagConstraints();
		c.gridwidth = GridBagConstraints.RELATIVE;
		makeButton(purshaceBisnesWithBallsBtn, layout, c);
		//this.add(purshaceBisnesWithBallsBtn);
		c = new GridBagConstraints();
		c.gridwidth = GridBagConstraints.REMAINDER;
		makeButton(purshaceBisnesWithoutBallsBtn, layout, c);
		//this.add(purshaceBisnesWithoutBallsBtn);
		c = new GridBagConstraints();
		c.gridwidth = GridBagConstraints.RELATIVE;
		makeButton(purshaceEconomWithBallsBtn, layout, c);
//		this.add(purshaceEconomWithBallsBtn);
		c = new GridBagConstraints();
		c.gridwidth = GridBagConstraints.REMAINDER;
		makeButton(purshaceEconomWithoutBallsBtn, layout, c);
		//this.add(purshaceEconomWithoutBallsBtn);
		this.pack();
		this.setVisible(true);
		
	}
	public JTextArea getInfoPassengerFlightBalls() {
		return infoPassengerFlightBalls;
	}
	public JButton getPurshaceBisnesWithBallsBtn() {
		return purshaceBisnesWithBallsBtn;
	}
	public JButton getPurshaceBisnesWithoutBallsBtn() {
		return purshaceBisnesWithoutBallsBtn;
	}
	public JButton getPurshaceEconomWithBallsBtn() {
		return purshaceEconomWithBallsBtn;
	}
	public JButton getPurshaceEconomWithoutBallsBtn() {
		return purshaceEconomWithoutBallsBtn;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		Integer countTickets = null;
		ResultSet res = null;
		String query = "select COUNT(*) from tickets;";
		try {
			res = allInfo.getSql().executeSqlQuery(query);
			while(res.next()){
				countTickets = Integer.valueOf(res.getString(1));
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		countTickets++;
		query = "insert into tickets values ('" + countTickets + "', '" + allInfo.getFlightId() + "', '" + 
				allInfo.getPassengerId() +"', '";
		if (e.getSource() == getPurshaceBisnesWithBallsBtn()){
			query += "true');";
			try {
				allInfo.getSql().executeSqlUpdate(query);
				clearBonus(allInfo.getPassengerId(), allInfo.getPriceBisnesTicketWithoutBonus());
				//updateBonus(allInfo.getPassengerId(), allInfo.getPriceBisnesTicketWithBonus());
				JOptionPane.showConfirmDialog(this, "You bought a ticket for " + allInfo.getPriceBisnesTicketWithBonus(), "Buying a ticket was successful!", JOptionPane.OK_CANCEL_OPTION);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		if (e.getSource() == getPurshaceBisnesWithoutBallsBtn()){
			query += "true');";
			try {
				allInfo.getSql().executeSqlUpdate(query);
				updateBonus(allInfo.getPassengerId(), allInfo.getPriceBisnesTicketWithoutBonus());
				JOptionPane.showConfirmDialog(this, "You bought a ticket for " + allInfo.getPriceBisnesTicketWithoutBonus(), "Buying a ticket was successful!", JOptionPane.OK_CANCEL_OPTION);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		}
		if (e.getSource() == getPurshaceEconomWithBallsBtn()){
			query += "false');";
			try {
				allInfo.getSql().executeSqlUpdate(query);
				clearBonus(allInfo.getPassengerId(), allInfo.getPriceEconomTicketWithoutBonus());
				//updateBonus(allInfo.getPassengerId(), allInfo.getPriceEconomTicketWithBonus());
				JOptionPane.showConfirmDialog(this, "You bought a ticket for " + allInfo.getPriceEconomTicketWithBonus(), "Buying a ticket was successful!", JOptionPane.OK_CANCEL_OPTION);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		}
		if (e.getSource() == getPurshaceEconomWithoutBallsBtn()){
			query += "false');";
			try {
				allInfo.getSql().executeSqlUpdate(query);
				updateBonus(allInfo.getPassengerId(), allInfo.getPriceEconomTicketWithoutBonus());
				JOptionPane.showConfirmDialog(this, "You bought a ticket for " + allInfo.getPriceEconomTicketWithoutBonus(), "Buying a ticket was successful!", JOptionPane.OK_CANCEL_OPTION);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		}

		this.setVisible(false);
	}
	
	
}
